﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Runtime.Serialization;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace elr_tracking_bal.CAT
{
    public class Proveedor : INotifyPropertyChanged
    {
        private int _ID_PROVEEDOR;
        private string _PROVEEDOR;
        private string _CONTACTO;
        private string _TEL1;
        private string _TEL2;
        private string _MAIL;
        private string _CALLE;
        private int _ID_PAIS;
        private int _ID_CIUDAD;
        private string _CODIGO_POSTAL;
        private string _RFC;

        public int ID_PROVEEDOR
        {
            get
            {
                return _ID_PROVEEDOR;
            }
            set
            {
                if (_ID_PROVEEDOR != value)
                {
                    _ID_PROVEEDOR = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ID_PROVEEDOR"));
                    }
                }
            }
        }

        public string PROVEEDOR
        {
            get
            {
                return _PROVEEDOR;
            }
            set
            {
                if (_PROVEEDOR != value)
                {
                    _PROVEEDOR = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("PROVEEDOR"));
                    }
                }
            }
        }

        public string CONTACTO
        {
            get
            {
                return _CONTACTO;
            }
            set
            {
                if (_CONTACTO != value)
                {
                    _CONTACTO = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("CONTACTO"));
                    }
                }
            }
        }

        public string TEL1
        {
            get
            {
                return _TEL1;
            }
            set
            {
                if (_TEL1 != value)
                {
                    _TEL1 = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("TEL1"));
                    }
                }
            }
        }

        public string TEL2
        {
            get
            {
                return _TEL2;
            }
            set
            {
                if (_TEL2 != value)
                {
                    _TEL2 = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("TEL2"));
                    }
                }
            }
        }

        public string MAIL
        {
            get
            {
                return _MAIL;
            }
            set
            {
                if (_MAIL != value)
                {
                    _MAIL = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("MAIL"));
                    }
                }
            }
        }

        public string CALLE
        {
            get
            {
                return _CALLE;
            }
            set
            {
                if (_CALLE != value)
                {
                    _CALLE = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("CALLE"));
                    }
                }
            }
        }

        public int ID_PAIS
        {
            get
            {
                return _ID_PAIS;
            }
            set
            {
                if (_ID_PAIS != value)
                {
                    _ID_PAIS = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ID_PAIS"));
                    }
                }
            }
        }

        public int ID_CIUDAD
        {
            get
            {
                return _ID_CIUDAD;
            }
            set
            {
                if (_ID_CIUDAD != value)
                {
                    _ID_CIUDAD = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("ID_CIUDAD"));
                    }
                }
            }
        }

        public string CODIGO_POSTAL
        {
            get
            {
                return _CODIGO_POSTAL;
            }
            set
            {
                if (_CODIGO_POSTAL != value)
                {
                    _CODIGO_POSTAL = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("CODIGO_POSTAL"));
                    }
                }
            }
        }

        public string RFC
        {
            get
            {
                return _RFC;
            }
            set
            {
                if (_RFC != value)
                {
                    _RFC = value;
                    if (this.PropertyChanged != null)
                    {
                        this.PropertyChanged(this, new PropertyChangedEventArgs("RFC"));
                    }
                }
            }
        }
        /// <summary>
        /// Método que ejecuta el store para obtener los Proveedores
        /// </summary>
        /// <returns></returns>
        public static DataTable GetData()
        {
            DataTable dt;
            dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_GETLIST_CAT_PROVEEDOR");
            return dt;
        }
        /// <summary>
        /// Método para obtener la lista de Proveedores
        /// </summary>
        /// <returns></returns>
        /// 

        public static ObservableCollection<Proveedor> GetNombreProveedor()
        {
            try
            {
                DataTable dt;
                dt = Proveedor.GetData();
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    ObservableCollection<Proveedor> list = new ObservableCollection<Proveedor>();
                    for (int x = 0; x < dt.Rows.Count; x++)
                    {
                        int id_Prov = Convert.ToInt32(dt.Rows[x][0]);
                        string proveedor = dt.Rows[x][1].ToString();

                        Proveedor prov = new Proveedor(id_Prov, proveedor);
                        list.Add(prov);

                    }

                    return list;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }

        public static List<Proveedor> GetDataProveedor()
        {
            try
            {
                DataTable dt;
                dt = Proveedor.GetData();
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else
                {
                    
                    List<Proveedor> list = dt.AsEnumerable().Select(
                        row => new Proveedor
                        {
                            ID_PROVEEDOR = row.Field<int?>("ID_PROVEEDOR").GetValueOrDefault(),
                            PROVEEDOR = String.IsNullOrEmpty(row.Field<string>("PROVEEDOR")) ? "" : row.Field<string>("PROVEEDOR"),
                            CONTACTO = String.IsNullOrEmpty(row.Field<string>("CONTACTO")) ? "" : row.Field<string>("CONTACTO"),
                            TEL1 = String.IsNullOrEmpty(row.Field<string>("TEL1")) ? "" : row.Field<string>("TEL1"),
                            TEL2 = String.IsNullOrEmpty(row.Field<string>("TEL2")) ? "" : row.Field<string>("TEL2"),
                            MAIL = String.IsNullOrEmpty(row.Field<string>("MAIL")) ? "" : row.Field<string>("MAIL"),
                            CALLE = String.IsNullOrEmpty(row.Field<string>("CALLE")) ? "" : row.Field<string>("CALLE"),
                            ID_PAIS = row.Field<int?>("ID_PAIS").GetValueOrDefault(),
                            ID_CIUDAD = row.Field<int?>("ID_CIUDAD").GetValueOrDefault(),
                            CODIGO_POSTAL = String.IsNullOrEmpty(row.Field<string>("CODIGO_POSTAL")) ? "" : row.Field<string>("CODIGO_POSTAL"),
                            RFC = String.IsNullOrEmpty(row.Field<string>("RFC")) ? "" : row.Field<string>("RFC"),
                        }
                        ).ToList();
                    return list;
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Método para insertar un Proveedor
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg InsertProveedor(Proveedor cat)
        {
            Proveedor inser;
            elr_tracking_bal.UpsertStateMsg inserCat;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_INSERT_CAT_PROVEEDOR '" + cat.PROVEEDOR + "','" + cat.CONTACTO + "','"
                                               + cat.TEL1 + "','" + cat.TEL2 + "','" + cat.MAIL + "','"
                                               + cat.CALLE + "'," + cat.ID_PAIS + "," + cat.ID_CIUDAD + ",'"
                                               + cat.CODIGO_POSTAL + "','" + cat.RFC + "'");

                inser = new Proveedor(cat.PROVEEDOR, cat.CONTACTO, cat.TEL1, cat.TEL2, cat.MAIL, cat.CALLE
                                        , cat.ID_PAIS, cat.ID_CIUDAD, cat.CODIGO_POSTAL, cat.RFC);
                inserCat = new elr_tracking_bal.UpsertStateMsg("Registros guardados " + dt.ToString(), true);
                return inserCat;
            }
            catch (Exception ex)
            {
                inserCat = new elr_tracking_bal.UpsertStateMsg("Error al guardar " + ex, false);
                return inserCat;
            }
        }
        /// <summary>
        /// Método para eliminar Proveedores
        /// </summary>
        /// <param name="ID_PROVEEDOR"></param>
        /// <returns></returns>
        public static elr_tracking_bal.DeleteStateMsg DeleteProveedor(string[] ID_PROVEEDOR)
        {
            elr_tracking_bal.DeleteStateMsg delete;
            string join;
            if (ID_PROVEEDOR == null)
            {
                join = "";
            }
            else
            {
                join = String.Join(",", ID_PROVEEDOR);
            }
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_DELETE_CAT_PROVEEDOR '" + join + "'");
                delete = new elr_tracking_bal.DeleteStateMsg("Registro eliminado" + dt.ToString(), true);
                return delete;
            }
            catch (Exception ex)
            {
                delete = new elr_tracking_bal.DeleteStateMsg("Error al eliminar" + ex, false);
                return delete;
            }
        }
        /// <summary>
        /// Método que actualiza un Proveedor
        /// </summary>
        /// <param name="cat"></param>
        /// <returns></returns>
        public static elr_tracking_bal.UpsertStateMsg UpdateProveedor(Proveedor cat)
        {
            Proveedor update;
            UpsertStateMsg updateCat;
            try
            {
                int dt = elr_tracking_dal.SrvDB.ExecuteNonQuery("exec SP_UPDATE_CAT_PROVEEDOR " + cat.ID_PROVEEDOR + ",'" + cat.PROVEEDOR 
                                        + "','" + cat.CONTACTO +"','"+cat.TEL1+"','"+cat.TEL2+"','"+cat.MAIL+"','"+cat.CALLE
                                        +"',"+cat.ID_PAIS+","+cat.ID_CIUDAD+",'"+cat.CODIGO_POSTAL+"','"+cat.RFC+ "'");
                update = new Proveedor(cat.ID_PROVEEDOR, cat.PROVEEDOR,cat.CONTACTO,cat.TEL1,cat.TEL2, cat.MAIL, cat.CALLE, cat.ID_PAIS, cat.ID_CIUDAD, cat.CODIGO_POSTAL, cat.RFC);
                updateCat = new UpsertStateMsg("Registros actualizados " + dt.ToString(), true);
                return updateCat;
            }
            catch (Exception ex)
            {
                updateCat = new UpsertStateMsg("Error al actualizar " + ex, false);
                return updateCat;
            }
        }
        /// <summary>
        /// Método que obtiene un Proveedor
        /// </summary>
        /// <param name="ID_PROVEEDOR"></param>
        /// <returns></returns>
        public static Proveedor GetOneProveedor(int ID_PROVEEDOR)
        {
            try
            {
                Proveedor cat;
                DataTable dt = elr_tracking_dal.SrvDB.ExecuteQuery("exec SP_GETONE_CAT_PROVEEDOR " + ID_PROVEEDOR + "");
                if (dt.Rows.Count == 0)
                {
                    return null;
                }
                else if (ID_PROVEEDOR == 0)
                {
                    return null;
                }
                else
                {
                    cat = new Proveedor(Convert.ToInt32(dt.Rows[0]["ID_PROVEEDOR"]),Convert.ToString(dt.Rows[0]["PROVEEDOR"]),Convert.ToString(dt.Rows[0]["CONTACTO"]),Convert.ToString(dt.Rows[0]["TEL1"]),Convert.ToString(dt.Rows[0]["TEL2"]), Convert.ToString(dt.Rows[0]["MAIL"]),
                        Convert.ToString(dt.Rows[0]["CALLE"]),Convert.ToInt32(dt.Rows[0]["ID_PAIS"]), Convert.ToInt32(dt.Rows[0]["ID_CIUDAD"]),Convert.ToString(dt.Rows[0]["CODIGO_POSTAL"]),Convert.ToString(dt.Rows[0]["RFC"]));
                       
                }
                return cat;
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
        /// <summary>
        /// Constructor sin parámetros
        /// </summary>
        public Proveedor() { }
        /// <summary>
        /// Constructor con dos parámetros
        /// </summary>
        /// <param name="ID_PROVEEDOR"></param>
        /// <param name="PROVEEDOR"></param>
        public Proveedor(int ID_PROVEEDOR, string PROVEEDOR)
        {
            this._ID_PROVEEDOR = ID_PROVEEDOR;
            this._PROVEEDOR = PROVEEDOR;
        }
        /// <summary>
        /// Constructor con diez parámetros
        /// </summary>
        /// <param name="PROVEEDOR"></param>
        /// <param name="CONTACTO"></param>
        /// <param name="TEL1"></param>
        /// <param name="TEL2"></param>
        /// <param name="MAIL"></param>
        /// <param name="CALLE"></param>
        /// <param name="ID_PAIS"></param>
        /// <param name="ID_CIUDAD"></param>
        /// <param name="CODIGO_POSTAL"></param>
        /// <param name="RFC"></param>
        public Proveedor(string PROVEEDOR, string CONTACTO, string TEL1, string TEL2, string MAIL, string CALLE
                                , int ID_PAIS, int ID_CIUDAD, string CODIGO_POSTAL, string RFC)
        {
            this._PROVEEDOR = PROVEEDOR;
            this._CONTACTO = CONTACTO;
            this._TEL1 = TEL1;
            this._TEL2 = TEL2;
            this._MAIL = MAIL;
            this._CALLE = CALLE;
            this._ID_PAIS = ID_PAIS;
            this._ID_CIUDAD = ID_CIUDAD;
            this._CODIGO_POSTAL = CODIGO_POSTAL;
            this._RFC = RFC;
        }
        /// <summary>
        /// Constructor con once parámetros
        /// </summary>
        /// <param name="ID_PROVEEDOR"></param>
        /// <param name="PROVEEDOR"></param>
        /// <param name="CONTACTO"></param>
        /// <param name="TEL1"></param>
        /// <param name="TEL2"></param>
        /// <param name="MAIL"></param>
        /// <param name="CALLE"></param>
        /// <param name="ID_PAIS"></param>
        /// <param name="ID_CIUDAD"></param>
        /// <param name="CODIGO_POSTAL"></param>
        /// <param name="RFC"></param>
        public Proveedor(int ID_PROVEEDOR, string PROVEEDOR, string CONTACTO, string TEL1, string TEL2, string MAIL, string CALLE
                                , int ID_PAIS, int ID_CIUDAD, string CODIGO_POSTAL, string RFC)
        {
            this._ID_PROVEEDOR = ID_PROVEEDOR;
            this._PROVEEDOR = PROVEEDOR;
            this._CONTACTO = CONTACTO;
            this._TEL1 = TEL1;
            this._TEL2 = TEL2;
            this._MAIL = MAIL;
            this._CALLE = CALLE;
            this._ID_PAIS = ID_PAIS;
            this._ID_CIUDAD = ID_CIUDAD;
            this._CODIGO_POSTAL = CODIGO_POSTAL;
            this._RFC = RFC;
        }

        public event PropertyChangedEventHandler PropertyChanged;
    }
}